<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>管理系统</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggleCollapse" @click="toggleColl">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          router
          :default-active="activeColor"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuLists"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveActiveColor('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单栏数据
      menuLists: [],
      iconObj: {
        125: 'el-icon-user-solid',
        102: 'el-icon-s-goods',
        103: 'el-icon-question',
        101: 'el-icon-circle-plus',
        145: 'el-icon-upload'
      },
      isCollapse: false,
      activeColor: ''
    }
  },
  created() {
    this.getMenuList()
    this.activeColor = window.sessionStorage.getItem('activeColor')
  },
  methods: {
    loginout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuLists = res.data
    },
    toggleColl() {
      this.isCollapse = !this.isCollapse
    },
    saveActiveColor(activecol) {
      window.sessionStorage.setItem('activeColor', activecol)
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  div {
    display: flex;
    align-items: center;
    img {
      width: 30px;
      height: 30px;
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.toggleCollapse {
  background-color: #4f575c;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
</style>
